package ar.edu.unq.tpi.uis.tepe3.wicket.components;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import java.util.List;

import ar.edu.unq.tpi.uis.tepe3.dominio.User;
import ar.edu.unq.tpi.uis.tepe3.wicket.model.UserList;
import ar.edu.unq.tpi.uis.tepe3.wicket.pages.NewUserPage;
import ar.edu.unq.tpi.uis.tepe3.wicket.pages.UserToGroupPage;

@SuppressWarnings("serial")
public class UserListView extends ListView {
	private Component resultSection;
	private UserList userList;

	public UserListView(String id, Component resultSection, Object object) {
		super(id);
		this.resultSection = resultSection;
		this.userList = (UserList) object;
	}

	@Override
	protected void populateItem(ListItem item) {
		//item.add(new Label("id"));
		item.add(new Label("name"));
		item.add(new Label("pass"));
		
		item.add(new Link("addGroup") {
			@Override
			public void onClick() {
				User user = (User) this.getParent().getModelObject();
				setResponsePage(new UserToGroupPage(user));
			}
		});
		
		item.add(new AjaxLink("delete") {
			@Override
			public void onClick(AjaxRequestTarget target) {
				User user = (User) this.getParent().getModelObject();
				UserListView.this.userList.delete(user);

				target.addComponent(UserListView.this.resultSection);
			}
		});
		
		item.add(new Link("edit") {
			@Override
			public void onClick() {
				User user = (User) this.getParent().getModelObject();
				setResponsePage(new NewUserPage(user));
			}
		});
	}
	
	protected IModel getListItemModel(IModel listViewModel, int index) {
		return new CompoundPropertyModel(((List<?>) listViewModel.getObject()).get(index));
	}

}

